package cn.com.gridinfo.sooc.core.expand.score.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.score.bean.Counts;

/**
 * 积分记录 Adapter
 *
 * @author yingchunli
 */
public class TxCountsAdapter extends UltimateViewAdapter<TxCountsAdapter.ViewHolder> {

    public List<Counts> countsInfoList;
    Context context;

    public TxCountsAdapter(List<Counts> countsInfoList, Context context) {
        this.countsInfoList = countsInfoList;
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_score_tx_item, parent, false);

        return new ViewHolder(view, true);
    }


    @Override
    public int getAdapterItemCount() {
        return countsInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= countsInfoList.size() : position < countsInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            Counts counts = countsInfoList.get(position);
            holder.time.setText(counts.getTime());//设置时间
            holder.count.setText(counts.getCount() + "元");//设置金额
            holder.count.setTextColor(context.getResources().getColor(R.color.color_tv333));
            holder.success.setText(counts.getType());
            if (counts.getType().equals("成功")) {
                //成功时，设置成功，文字为绿色
                holder.reason.setVisibility(View.GONE);
                holder.success.setTextColor(context.getResources().getColor(R.color.green_score));//设置为绿色字
            } else if (counts.getType().equals("失败")) {

                //失败时，设置文字为红色，显示失败原因
                holder.reason.setVisibility(View.VISIBLE);
                holder.reason.setText("失败原因: " + counts.getReason());
                holder.success.setTextColor(context.getResources().getColor(R.color.red_hint));//设置为红色字
            } else {
                //
                holder.reason.setVisibility(View.GONE);
                holder.success.setTextColor(context.getResources().getColor(R.color.red_hint));//设置为红色字
            }
            //底部分割线的显示
//            if (position == countsInfoList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    public void loadmore(List<Counts> list) {
        countsInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Counts> list) {
        countsInfoList.clear();
        countsInfoList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView time, count, success, reason;
        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                time = (TextView) itemView.findViewById(R.id.time_score);//时间
                count = (TextView) itemView.findViewById(R.id.count_score);//金额
                success = (TextView) itemView.findViewById(R.id.success_score);//成功与否  只用于提现记录
                reason = (TextView) itemView.findViewById(R.id.reason_score);//原因
//                line = itemView.findViewById(R.id.line);//底部分割线
            }
        }
    }
}
